# -*- coding:utf-8 -*-
# requests.post()
# 语法
"""
requests.post(url, data=None)
requests.post(url, json=None)
"""

import requests
import json

url = "https://fanyi.baidu.com/v2transapi?from=zh&to=en"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
    "Referer": "https://fanyi.baidu.com/",
    "Host": "fanyi.baidu.com",
    "Cookie": 'BAIDUID=41B7D0E7FD164B7DC20D7AF82EFEAEA5:FG=1; H_WISE_SIDS=219946_131862_216837_213354_214805_219942_213039_204915_110085_236308_243886_244476_244718_240590_245412_247148_250889_251428_249892_254592_253426_255981_256083_255661_254835_256739_251973_256620_257081_257102_254075_257289_254317_251058_257661_257936_257167_257904_257823_257586_253900_258253_257994_255224_258376_227147_258723_258728_258938_257302_258985_258958_258694_230288_259047_257016_107316_252256_259191_259193_259277_256223_259403_259286_259391_259431_259536_228132_259569_259605_259628_259581_256998_259558_259653_258773_234296_234208_259888_259642_255909_260045_254300_260125_259958_260235_256229_260355_260390_260362_259187_260457_253022_255212_260569_258081_260334_259408_260651_260352_251786_253676_260834_259312_259422_259583_260718_260976_260938_259031; BIDUPSID=41B7D0E7FD164B7DC20D7AF82EFEAEA5; PSTM=1684829720; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; H_WISE_SIDS_BFESS=219946_131862_216837_213354_214805_219942_213039_204915_110085_236308_243886_244476_244718_240590_245412_247148_250889_251428_249892_254592_253426_255981_256083_255661_254835_256739_251973_256620_257081_257102_254075_257289_254317_251058_257661_257936_257167_257904_257823_257586_253900_258253_257994_255224_258376_227147_258723_258728_258938_257302_258985_258958_258694_230288_259047_257016_107316_252256_259191_259193_259277_256223_259403_259286_259391_259431_259536_228132_259569_259605_259628_259581_256998_259558_259653_258773_234296_234208_259888_259642_255909_260045_254300_260125_259958_260235_256229_260355_260390_260362_259187_260457_253022_255212_260569_258081_260334_259408_260651_260352_251786_253676_260834_259312_259422_259583_260718_260976_260938_259031; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BAIDUID_BFESS=41B7D0E7FD164B7DC20D7AF82EFEAEA5:FG=1; ZFY=o:BqOmgSMiP0dt9N1AFVKCa3VB0rMLFbcJ5cbjeo8DYY:C; MCITY=-106%3A; BAIDU_WISE_UID=wapp_1694760915819_874; RT="z=1&dm=baidu.com&si=ce42f8a6-6d2f-46d7-a070-0d839cf16fc4&ss=lmk8w2fn&sl=6&tt=777&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=dsj&ul=40ei&hd=40fe"; BA_HECTOR=048181810g0h2k8185a00g2r1ig85f51o; delPer=0; PSINO=5; H_PS_PSSID=39314_39366_39354_39392_39351_39097_39198_39261_39359_39374_39233_26350; APPGUIDE_10_6_2=1; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1694846505; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1694846841; ab_sr=1.0.1_MjMwYzFhMTg4MDcwMDdlMzEyNTVmYzZjNDNjZWU0MmI0NmRlY2QzN2UzMTM5OTJmMTMzMGJlY2JlZTVkN2VkZjc3NjliNjg1YThkMWZlOWJhNGZkZThiMWRmZWM1NzdlM2NkOTM1ZjVlODk5NDliZThkZjE3ZWVhZWZmZDA4YWJkN2E0MmU0MjYwOTQ5NmIzYjFiMzgzMzQyMjcyMWYyZA=='
}


data = {
    "from": "zh",
    "to": "en",
    "query": "你好",
    "transtype": "realtime",
    "simple_means_flag": "3",
    "sign": "232427.485594",
    "token": "deae1aa1fdbabdcb505c1cfc0f88c51d",
    "domain": "common",
    "ts": "1694846985063"
}

resp = requests.post(url, data=data, headers=headers)
# print(resp.text)          # 查看得到的是，什么类型
"""
# 如果数据由大括号｛｝包裹，则可以直接使用xx.json()将，
数据中的unicode编码转化，变为python对应的数据类型。
"""
# print(resp.json())
"""
结果如下：括号外还有，数据则不能直接.json转化
data: {'errno': 997, 'errmsg': '未知错误', 'logid': 1681836165, 
'query': '你好', 'from': 'zh', 'to': 'en', 'error': 997}
可以使用下面方法
"""
dict_data = json.loads(resp.text)
print(dict_data['trans_result']['data'][0]['dst'])
# print(type(dict_data))
# with open("ss.txt", "") as f:
#     f.write(dict_data)
